package net.sf.odinms.exttools.doclet;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * A simple GUI for providing actual Javadoc arguments for using XHTML Doclet.
 * Displays a dialog with instructions about doclet invocation, including the
 * exact parameters needed to use XHTML Doclet
 */
public final class DocletGUI {
	
	/** The amount of space to leave between GUI elements and window bounds. */
	private final int edgePadding = 15;
	/** The amount of space to leave between GUI elements . */
	private final int internalPadding = 10;
	
	/**
	 * Display a dialog with instructions about how to invoke this doclet.
	 * 
	 * @param args Any user-specified argument Strings.
	 * @see DocletGUI
	 */
	public static void main(String[] args) {
		new DocletGUI();
	}
	
	/**
	 * Displays a dialog with instructions about how to invoke this doclet.
	 * Specifies the correct Javadoc flags to use, and has a single default
	 * button to quit. Uses {@link SpringLayout} to make window flexible.
	 */
	@SuppressWarnings("unchecked")
	private DocletGUI() {
		JFrame frame = new JFrame("Invocation Error");
		Container content = frame.getContentPane();
		SpringLayout layout = new SpringLayout();
		content.setLayout(layout);
		
		// Create a warning icon for the upper left corner.
		JLabel icon = new JLabel(UIManager.getIcon("OptionPane.warningIcon"));
		content.add(icon);
		layout.putConstraint(SpringLayout.NORTH, icon, edgePadding, SpringLayout.NORTH,
				content);
		layout.putConstraint(SpringLayout.WEST, icon, edgePadding, SpringLayout.WEST,
				content);
		
		// Create and add text labels.
		JLabel label1 =
				new JLabel(
						"ERROR: You cannot execute a Javadoc doclet directly.");
		content.add(label1);
		layout.putConstraint(SpringLayout.WEST, label1, edgePadding, SpringLayout.EAST,
				icon);
		layout.putConstraint(SpringLayout.NORTH, label1, edgePadding,
				SpringLayout.NORTH, content);
		JLabel label2 =
				new JLabel("To use XHTML Doclet, add these Javadoc arguments:");
		content.add(label2);
		layout.putConstraint(SpringLayout.WEST, label2, edgePadding, SpringLayout.EAST,
				icon);
		layout.putConstraint(SpringLayout.NORTH, label2, 5, SpringLayout.SOUTH,
				label1);
		
		// Create and populate text area with the actual Javadoc parameters.
		JTextArea textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.append(" -docletpath ");
		Class c = Doclet.class;
		try {
			String uri =
					new java.net.URI(c.getResource("").getPath()).getSchemeSpecificPart();
			// Strip off the ! and following section from the URI
			textArea.append(uri.substring(0, uri.indexOf('!')));
		}
		catch (java.net.URISyntaxException exc) {
			textArea.append(c.getProtectionDomain().getCodeSource().getLocation().getPath());
		}
		textArea.append(" \n -doclet " + c.getName() + " ");
		content.add(textArea);
		textArea.setBorder(BorderFactory.createLineBorder(Color.gray));
		layout.putConstraint(SpringLayout.WEST, textArea, edgePadding,
				SpringLayout.EAST, icon);
		layout.putConstraint(SpringLayout.NORTH, textArea, internalPadding,
				SpringLayout.SOUTH, label2);
		
		// Create an OK button with anonymous listener class to quit
		JButton okButton = new JButton("OK");
		okButton.setDefaultCapable(true);
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		content.add(okButton);
		frame.getRootPane().setDefaultButton(okButton);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		layout.putConstraint(SpringLayout.SOUTH, okButton, -edgePadding,
				SpringLayout.SOUTH, content);
		layout.putConstraint(SpringLayout.EAST, okButton, -internalPadding,
				SpringLayout.EAST, content);
		
		// Set constraints of container
		layout.putConstraint(SpringLayout.SOUTH, content, 55,
				SpringLayout.SOUTH, textArea);
		layout.putConstraint(SpringLayout.EAST, content, edgePadding, SpringLayout.EAST,
				label1);
		layout.putConstraint(SpringLayout.EAST, content, edgePadding, SpringLayout.EAST,
				label2);
		layout.putConstraint(SpringLayout.EAST, content, edgePadding, SpringLayout.EAST,
				textArea);
		
		frame.pack();
		frame.setLocationRelativeTo(null); // Relative to desktop
		// frame.setResizable(false); // Unless we can set min size (short path)
		frame.setVisible(true);
	}
	
}
